package com.winit.schedule.task.tracker.quartz;

public class TriggerStatisticsInfo {

    private String schedulerInstanceId;

    private String triggerName;

    private String triggerGroup;

    private long   minJobRuntime;

    private long   maxJobRuntime;

    /**
     * 近似值，非绝对值
     */
    private long   avgJobRuntime;

    private long   lastTriggerTime;

    private int    runCount;

    public TriggerStatisticsInfo(String schedulerInstanceId, String triggerName, String triggerGroup){
        super();
        this.schedulerInstanceId = schedulerInstanceId;
        this.triggerName = triggerName;
        this.triggerGroup = triggerGroup;
    }

    public int getRunCount() {
        return runCount;
    }

    public long getMinJobRuntime() {
        return minJobRuntime;
    }

    public long getMaxJobRuntime() {
        return maxJobRuntime;
    }

    public long getAvgJobRuntime() {
        return avgJobRuntime;
    }

    public synchronized void addJobRuntime(long jobRuntime) {
        minJobRuntime = Math.min(jobRuntime, minJobRuntime == 0 ? Integer.MAX_VALUE : minJobRuntime);
        maxJobRuntime = Math.max(jobRuntime, maxJobRuntime);
        avgJobRuntime = ((avgJobRuntime * runCount++) + jobRuntime) / runCount;

    }

    public long getLastTriggerTime() {
        return lastTriggerTime;
    }

    public void setLastTriggerTime(long lastTriggerTime) {
        this.lastTriggerTime = lastTriggerTime;
    }

    public String getSchedulerInstanceId() {
        return schedulerInstanceId;
    }

    public String getTriggerName() {
        return triggerName;
    }

    public String getTriggerGroup() {
        return triggerGroup;
    }

    public static void main(String[] args) {
        int count = 1;
        System.out.println((2 * count++ + 1) / count);
    }

    public String getKey() {
        return this.schedulerInstanceId + "." + this.triggerGroup + "." + this.triggerName;
    }

}
